package com.cloudlinkscm.base.filecenter.dao;

import com.cloudlinkscm.base.filecenter.mapper.FileRecordMapper;
import com.cloudlinkscm.base.filecenter.model.FileRecord;
import com.cloudlinkscm.loms.framework.dao.GenericBizDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collection;
import java.util.List;

/**
 * Created by syq on 2017/7/22.
 */
@Repository
public class FileRecordDao extends GenericBizDao<FileRecord, String> {
    private FileRecordMapper fileRecordMapper;

    @Autowired
    public FileRecordDao(FileRecordMapper mapper) {
        super(mapper);
        this.fileRecordMapper = mapper;
    }

    /**
     * 查询指定文件ID对应文件记录
     *
     * @param fileIds
     * @return
     */
    public List<FileRecord> listByIds(Collection<String> fileIds) {
        if (CollectionUtils.isEmpty(fileIds)) {
            return null;
        }
        Example example = new Example(FileRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", fileIds);
        return selectByExample(example);
    }

    /**
     * @param fileIds
     * @return
     */
    public int deleteByIds(Collection<String> fileIds) {
        if (CollectionUtils.isEmpty(fileIds)) {
            return 0;
        }
        Example example = new Example(FileRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", fileIds);
        FileRecord record = new FileRecord();
        record.setDelete(true);
        return updateByExampleSelective(record, example);
    }

    @Override
    protected void setBoundary(FileRecord criteria) {
        super.setBoundary(criteria);
        criteria.setDelete(false);
    }

    @Override
    protected void setBoundaryCriteria(Example.Criteria criteria) {
        super.setBoundaryCriteria(criteria);
        criteria.andEqualTo("isDelete", false);
    }
}
